package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.api.PaymentClient;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class OrderFeignController {

    @Autowired
    private PaymentClient paymentClient;

    @GetMapping("/feign/get/payment/{id}")
    public CommonResult<Payment> getPayment(@PathVariable Long id, HttpServletRequest request) {

        CommonResult<Payment> commonResult = paymentClient.getPaymentById(id);

        int serverPort = request.getServerPort();
        String serial = commonResult.getData().getSerial() + " " + serverPort;
        commonResult.getData().setSerial(serial);

        return commonResult;
    }

    @PostMapping("/feign/save/payment")
    public CommonResult<Integer> savePayment(Payment payment) {

        CommonResult<Integer> commonResult = paymentClient.create(payment);

        return commonResult;
    }

}
